package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * @author hcz
 * @describtion TODO
 * @date 2019-06-25 15:47
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤条件
        Example example = new Example(Brand.class);
        /*select * from tb_brand where letter like 'H' or `name` like 'H'
           ORDER BY id desc LIMIT 0,5*/
        if (StringUtils.isNoneBlank(key)) {
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }

        //排序
        if (StringUtils.isNoneBlank(sortBy)) {
            String orderBycluse = sortBy + (desc ? " DESC":" ASC");
           example.setOrderByClause(orderBycluse);
        }

        //查询
        List<Brand> brands = brandMapper.selectByExample(example);

        //判断是否为空
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        //解析分页数据
        PageInfo<Brand> info = new PageInfo<>(brands);

        //转换成BrandDTO
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        //返回
        return new PageResult<>(info.getTotal(),brandDTOS);
    }

    /**
     * 品牌新增
     * @param brandDTO
     * @param ids
     */
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> ids) {
        //新增品牌
        Brand brand = BeanHelper.copyProperties(brandDTO,Brand.class);
        brand.setId(null);
        int i = brandMapper.insertSelective(brand);
        if (i != 1) {
            //新增失败
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增品牌和种类中间表
        i = brandMapper.insertCategoryBrand(brand.getId(),ids);
        if (i!=ids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 通过id找到一个品牌对象
     * @param id
     * @return
     */
    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }
}
